package com.mystudy.network;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @program: infoalgorithm
 * @description: 简单的服务端
 * @author: zhouzhilong
 * @create: 2019-05-24 16:14
 **/
public class SimpleSocketServer {
    public static void main(String[] args) throws IOException {
        Socket socket = null;
        OutputStream output = null;
        InputStream input = null;
        int port = 10000;
        try (ServerSocket serverSocket = new ServerSocket(port)) {
            System.out.println("before accept");
            socket = serverSocket.accept();
            System.out.println("after accept");

            System.out.println("before getInputStream");
            input = socket.getInputStream();
            System.out.println("after getInputStream");


            byte[] b = new byte[1024];
            System.out.println("before read");
            for(int i = 0;i <5;i++){
                int n = input.read(b);
                System.out.println(n);
                System.out.println("after read");
                if(n >=0){
                    System.out.println("客户端发送内容为：" + new String(b, 0, n));

                    //输出
                    //向客户端发送反馈内容
                    output = socket.getOutputStream();
                    output.write(b, 0, n);
                }



            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            input.close();
            output.close();
            socket.close();
        }

    }
}
